package b2.widget.rank
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.BFont;
	import b2.widget.*;
	import b2.widget.tank.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class RankingItem extends Sprite
	{
		private const RANK_COLOR:Array = [0xFF0000, 0xA0A000, 0x00A000, 0x000000];
		private const RANK_NAME:Array = ["st", "nd", "rd", "th"];
		private var rankingInfo:RankingInfo;
		
		public function RankingItem(ranking:int, rankingInfo:RankingInfo)
		{
			super();
			//坦克名字
			var tankConfig:TankConfig = Config.objectConfigs[rankingInfo.tankIndex];
			var basicLevelName:String = Config.basicConfig.objectBasicLevel[rankingInfo.basicLevel];
			var strengthenLevelName:String = Config.basicConfig.objectStrengthenLevel[rankingInfo.strengthenLevel];
			//背景框
			var RankingItemBgr:Class = ResourceHelper.getInstance().getClassByName("RankingItemBgr");
			var rankingItemBgr:Sprite = new RankingItemBgr();
			addChild(rankingItemBgr);
			//排名
			var rankingText:TextField = new TextField();
			rankingText.selectable = false;
        	rankingText.autoSize = TextFieldAutoSize.CENTER;
        	var colorIndex:int = Math.min(ranking, 4) - 1;
			rankingText.defaultTextFormat = new TextFormat("微软雅黑", 20, RANK_COLOR[colorIndex], true);
			rankingText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			rankingText.text = ranking + RANK_NAME[colorIndex];
			rankingText.x = 20;
			rankingText.y = 15;
			addChild(rankingText);
			//背景框
			var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr" + rankingInfo.basicLevel);
			var objectItemBgr:Sprite = new ObjectItemBgr();
			objectItemBgr.x = 90;
			objectItemBgr.y = 9;
			objectItemBgr.width = 40;
			objectItemBgr.height = 40;
			addChild(objectItemBgr);
			//坦克图片
			var TankImage:Class = ResourceHelper.getInstance().getClassByName(tankConfig.iconImage);
			var tankSprite:Sprite = new TankImage();
			//齐天大圣
			if (rankingInfo.tankIndex == 10015)
			{
				tankSprite.width = 48;
				tankSprite.height = 54;
			}
			//死亡骑士
			else if (rankingInfo.tankIndex == 10020)
			{
				tankSprite.width = 42;
				tankSprite.height = 42;
			}
			else
			{
				tankSprite.width = 35;
				tankSprite.height = 35;
			}
			tankSprite.x = 92;
			tankSprite.y = 11;
			addChild(tankSprite);
			//用户名字
			var playerNameText:TextField = new TextField();
			playerNameText.selectable = false;
        	playerNameText.autoSize = TextFieldAutoSize.CENTER;
			playerNameText.defaultTextFormat = new TextFormat("微软雅黑", 14, RANK_COLOR[colorIndex], true);
			playerNameText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			playerNameText.text = rankingInfo.playerName;
			playerNameText.x = 148;
			playerNameText.y = 10;
			addChild(playerNameText);
			var tankNameText:TextField = new TextField();
			tankNameText.selectable = false;
        	tankNameText.autoSize = TextFieldAutoSize.CENTER;
			tankNameText.defaultTextFormat = new TextFormat("微软雅黑", 12, RANK_COLOR[colorIndex], true);
			tankNameText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			tankNameText.text = tankConfig.name + " · " + basicLevelName + strengthenLevelName;
			tankNameText.x = 148;
			tankNameText.y = 28;
			addChild(tankNameText);
			//数值
			var numText:TextField = new TextField();
			numText.selectable = false;
        	numText.autoSize = TextFieldAutoSize.CENTER;
        	var textFormat:TextFormat = new TextFormat("微软雅黑", 18, RANK_COLOR[colorIndex], true);
        	textFormat.italic = true;
			numText.defaultTextFormat = textFormat;
			numText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			numText.x = 350;
			numText.y = 13;
			//经验
			if (rankingInfo.objectIndex == CommonObjConfig.EXP)
			{
				var levelAndExp:Array = this.getLevel(rankingInfo.objectNum);
				numText.text = levelAndExp[0].toString() + "级 " + levelAndExp[1].toString() + "%  ";
			}
			//荣誉
			else if (rankingInfo.objectIndex == CommonObjConfig.HONOUR)
			{
				var honourInfo:Array = PlayerInfo.getTitleInfo(rankingInfo.objectNum); 
				numText.text = honourInfo[2] + "  ";
			}
			else
			{
				numText.text = rankingInfo.objectNum.toString() + "  ";
			}
			addChild(numText);
			
			this.rankingInfo = rankingInfo;
			this.addEventListener(MouseEvent.CLICK, showTankInfo);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function getLevel(exp:int):Array
		{
			var level:int = 0;
			while (level < Config.levelConfigs.length && exp >= Config.levelConfigs[level].experience)
			{
				exp -= Config.levelConfigs[level].experience;
				level++;
			}
			level = level >= Config.levelConfigs.length ? Config.levelConfigs.length - 1 : level;
			var rate:int = Math.floor(exp * 100 / Config.levelConfigs[level].experience);
			return [level + 1, rate];
		}
		
		private function showTankInfo(event:MouseEvent):void
		{
			ServerHelper.getInstance().getPlayerInfo(this.rankingInfo.playerId);
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
		}
		
		private function onShowTankInfoComplete(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
			TankDialog.getInstance().fresh(this.rankingInfo.tankId);
			WidgetManager.getInstance().popOut([TankDialog.getInstance()], false, true);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
	}
}